<?php
/**
 * Created by PhpStorm.
 * User: zws
 * Date: 2018/9/8
 * Time: 11:02
 * 检测报告管理
 */
namespace app\index\service;

use app\common\model\ReportModel;
use think\Exception;

class ReportService
{
    protected static $reportModel;
    /**
     * UserService constructor.
     * @param ReportModel $reportModel
     * @author zws
     */
    public function __construct(ReportModel $reportModel)
    {
        self::$reportModel = $reportModel;
    }


    /**
     * 查询报告
     * @param $param
     * @return array
     * @author zws
     */
    public function index($param)
    {
        if(!isset($param['page']) || empty($param['page'])){
            $param['page'] = 1;
        }
        if(!isset($param['limit']) || empty($param['limit'])){
            $param['limit'] = 10;
        }
        $userId = $param['user_id'];
        $where[] = ['user_id', '=',  $userId];
        $where[] = ['is_del', '=', 1];
        if(isset($param['startTime']))
        {
            $where[] = ['create_time', '>=', date('Y-m-d H:i:s', $param['startTime'])];
        }
        if(isset($param['endTime']))
        {
            $where[] = ['endTime', '<=', date('Y-m-d H:i:s', $param['endTime'])];
        }
        if(isset($param['name']))
        {
            $where[] = ['report_name', "like", "%".$param['name']."%"];
        }
        // 获取总数
        $count = self::$reportModel->where($where)->count();
        if ($count < 1) {
            return ['data' => [], 'count' => $count];
        }
        $filePath = config('app.app_host').config('param_config.reportPath');
        $result = self::$reportModel->where($where)
            ->page($param['page'], $param['limit'])
            ->order('create_time', 'desc')
            ->select();
        if($result)
        {
            foreach ($result as $k=>$v)
            {
                $result[$k]->report_url = $filePath.$v->report_url;
            }
        }
        return ['data' => $result, 'count' => $count];
    }

    /**
     * 上传报告数据
     * @param $param
     * @author zws
     */
    public function uploadReport($param)
    {
        if(empty($param))
        {
            app_fail(9997);
        }
        if(!isset($param['order_id']) || empty($param['order_id']))
        {
            app_fail(6996);
        }
        if(!isset($param['name']) || empty($param['name']))
        {
            app_fail(6991);
        }
        if(!isset($param['report']) || empty($param['report']))
        {
            app_fail(6990);
        }
        try{
            $file = $param['report'];
            $data['order_id'] = $param['order_id'];
            $data['report_name'] = $param['name'];
            $data['user_id'] = $param['user_id'];
            $reportPath = config('param_config.reportPath');
            // 移动到框架应用根目录/uploads/ 目录下
            $info = $file->move($reportPath);
            if($info){
                $data['report_url'] = $info->getSaveName();
            }else{
                // 上传失败获取错误信息
                app_fail($file->getError());
            }
            self::$reportModel->save($data);
        }catch (\Exception $e){
            app_fail(9899);
        }
    }

    /**
     * 获取报告地址
     * @param $param
     * @return string
     * @author zws
     */
    public function downloadReport($param)
    {
        if(empty($param))
        {
            app_fail(9997);
        }
        if(!isset($param['report_id']) || empty($param['report_id']))
        {
            app_fail(6988);
        }
        $reportData = self::$reportModel->where('id', '=', $param['report_id'])->find();
        if(!$reportData)
        {
            app_fail(6987);
        }
        $filePath = config('app.app_host').config('param_config.reportPath');
        if($reportData->report_url)
        {
            return ['url'=>$filePath.$reportData->report_url];
        }
        return '';
    }

}